import 'dart:io';
import 'package:base_lib/common/index.dart';
import 'package:base_lib/index.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:image_picker/image_picker.dart';

///图片选择控件
class BaseImgPicker<T> extends BaseThemeWidget {
  final Function(String) onPreview;
  final Function(String) onSuccess;
  final Function(dynamic) onFailure;
  final Function(T) onDelete;
  final bool isShowDeleteDialog;
  final String tip;
  final bool showLoading;
  final String imagePath;
  final double height;
  final double width;
  final T data;

  BaseImgPicker(
    this.data, {
    this.onPreview,
    this.onSuccess,
    this.onFailure,
    this.onDelete,
    this.isShowDeleteDialog = true,
    this.tip = '',
    this.showLoading = false,
    this.imagePath = '',
    this.height = 80.0,
    this.width = 80.0,
  });

  void getPic(bool isCamera) async {
    PickedFile pickedFile;
    try {
      pickedFile = await ImagePicker().getImage(
          source: isCamera ? ImageSource.camera : ImageSource.gallery);
    } catch (e) {
      onFailure?.call(e);
      return;
    }
    onSuccess?.call(pickedFile.path);
  }

  @override
  Widget buildChild(BuildContext context, MTheme theme) {
    bool noImagePath = imagePath.isEmpty;
    return Container(
      alignment: Alignment.center,
      width: width,
      height: height,
      child: GestureDetector(
        onTap: () async {
          showMyBottomSheet(
              context: context,
              builder: (context) {
                return Container(
                  decoration: BoxDecoration(
                    borderRadius: BorderRadius.only(
                        topLeft: Radius.circular(20),
                        topRight: Radius.circular(20)),
                    color: Colors.transparent,
                  ),
                  // height: ScreenAdapter.screenHeightDp * 0.4,
                  child: Column(
                    mainAxisAlignment: MainAxisAlignment.end,
                    children: [
                      40.vGapDp,
                      GestureDetector(
                        onTap: () {
                          getPic(true);
                          Navigator.pop(context);
                        },
                        child: Container(
                          margin: EdgeInsets.symmetric(horizontal: 10),
                          decoration: BoxDecoration(
                            borderRadius: BorderRadius.all(Radius.circular(20)),
                            color: Colors.white,
                          ),
                          alignment: Alignment.center,
                          width: double.infinity,
                          height: 40,
                          child: Text('拍照'),
                        ),
                      ),
                      20.vGapDp,
                      GestureDetector(
                        onTap: () {
                          getPic(false);
                          Navigator.pop(context);
                        },
                        child: Container(
                          margin: EdgeInsets.symmetric(horizontal: 10),
                          alignment: Alignment.center,
                          decoration: BoxDecoration(
                            borderRadius: BorderRadius.all(Radius.circular(20)),
                            color: Colors.white,
                          ),
                          width: double.infinity,
                          height: 40,
                          child: Text('相册'),
                        ),
                      ),
                      40.vGapDp,
                      GestureDetector(
                        onTap: () {
                          Navigator.pop(context);
                        },
                        child: Container(
                          margin: EdgeInsets.symmetric(horizontal: 10),
                          alignment: Alignment.center,
                          decoration: BoxDecoration(
                            borderRadius: BorderRadius.all(Radius.circular(20)),
                            color: Colors.white,
                          ),
                          width: double.infinity,
                          height: 40,
                          child: Text('取消'),
                        ),
                      ),
                      40.vGapDp,
                    ],
                  ),
                );
              });
        },
        child: ClipRRect(
          borderRadius: BorderRadius.circular(5),
          child: showLoading
              ? buildLoadingIndicator()
              : Container(
                  width: width,
                  height: height,
                  color: Colors.grey[200],
                  child: noImagePath
                      ? Column(
                          children: [
                            Spacer(),
                            tip.isEmpty ? SizedBox() : Text(tip),
                            2.vGapDp,
                            Icon(Icons.add, color: Colors.grey),
                            Spacer(),
                          ],
                        )
                      : Stack(
                          fit: StackFit.loose,
                          children: [
                            GestureDetector(
                              onTap: () {
                                if (onPreview == null) {
                                  ShowImgDialogUtil.showImgDialog(
                                      context, imagePath);
                                } else {
                                  onPreview?.call(imagePath);
                                }
                              },
                              child: (imagePath.contains("http://") ||
                                      imagePath.contains("https://"))
                                  ? Image.network(imagePath)
                                  : Image.file(
                                      File(imagePath),
                                    ),
                            ),
                            Positioned(
                              top: 5,
                              right: 5,
                              child: GestureDetector(
                                onTap: () {
                                  isShowDeleteDialog
                                      ? Custom_iOS_Dialog.prompt(context,
                                          content: '确定删除 $tip 吗？', okFn: () {
                                          onDelete?.call(data);
                                        })
                                      : onDelete?.call(data);
                                },
                                child: Icon(
                                  Icons.delete_forever,
                                  color: Colors.red,
                                  size: 25,
                                ),
                              ),
                            ),
                          ],
                        ),
                ),
        ),
      ),
    );
  }
}
